/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
struct ListNode {
    int val;
    ListNode* next;
    ListNode() : val(0), next(nullptr) {}
    ListNode(int x) : val(x), next(nullptr) {}
    ListNode(int x, ListNode* next) : val(x), next(next) {}
};

class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        ListNode* cur1 = l1;
        ListNode* cur2 = l2;
        ListNode* head = new ListNode;
        ListNode* cur = head;
        int t = 0;
        while (cur1 || cur2)
        {
            t += cur2 != nullptr ? cur2->val : 0;
            t += cur1 != nullptr ? cur1->val : 0;
            ListNode* newnode = new ListNode;
            newnode->val = t % 10;
            t /= 10;
            cur->next = newnode;
            cur = newnode;
            if (cur1)  cur1 = cur1->next;
            if (cur2)  cur2 = cur2->next;
        }
        if (t > 0)
        {
            ListNode* newnode = new ListNode;
            newnode->val = t % 10;
            cur->next = newnode;
        }
        return head->next;
    }
};